package com.spring.oaassist.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.oa.api.web.dao.OaMapper;
import com.spring.common.web.dao.TracelogboMapper;
import com.spring.common.web.model.TracelogboWithBLOBs;
import com.spring.oaassist.web.dao.TeacherMapper;
import com.spring.oaassist.web.model.Teacher;
import com.spring.oaassist.web.service.TeacherService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UpdateMap;
import com.spring.security.web.dao.CommonMapper;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

@Service
public class TeacherServiceImpl implements TeacherService{
	
	@Resource private TeacherMapper teacherMapper;
	
	@Resource private OaMapper oaMapper;
	
	@Resource private CommonMapper commonMapper;
	
	@Resource private JedisCache jedisCache;
	
	@Resource TracelogboMapper tracelogboMapper;
	

	@Override
	public List<Map<String, Object>> queryTearcherList(Map<String, Object> parm) {
		return oaMapper.queryTearcherList(parm);
	}

	@Override
	public List<Teacher> queryTearcherById(String user_id) {
		return teacherMapper.queryTearcherById(user_id);
	}

	@Override
	public Map<String, Object> saveTeacherInfo(Map<String, Object> parm,HttpServletRequest request) {
		String type="修改";
		int setLogcategory=2;
		String user_id = ApplicationUtils.Object2String(parm.get("user_id"));
		List<Teacher> tl = teacherMapper.queryTearcherById(user_id);
		Map<String,Object> retM = Maps.newHashMap();
		retM.put(ApplicationUtils.STATUS, "1");
		retM.put(ApplicationUtils.MSG, "保存成功");
		if(tl!=null&&tl.size()>0){//更新
			UpdateMap up = new UpdateMap("t_teacher");
			up.addField("name", ApplicationUtils.Object2String(parm.get("user_name")));
			up.addField("sex", ApplicationUtils.isNotNull(ApplicationUtils.Object2String(parm.get("sex")))?ApplicationUtils.Object2String(parm.get("sex")):"1");
			up.addField("birthday", ApplicationUtils.Object2String(parm.get("birthday")));
			up.addField("`desc`", ApplicationUtils.Object2String(parm.get("desc")));
			up.addField("score", ApplicationUtils.isNotNull(ApplicationUtils.Object2String(parm.get("t_score")))?ApplicationUtils.Object2String(parm.get("t_score")):"0");
			up.addField("image_src", ApplicationUtils.Object2String(parm.get("image_src")));
			up.addField("title", ApplicationUtils.Object2String(parm.get("title")));
			up.addField("teach_glimpse", ApplicationUtils.Object2String(parm.get("teach_glimpse")));
			up.addField("dept_id", ApplicationUtils.Object2String(parm.get("dept_id")));
			up.addField("dept_name", ApplicationUtils.Object2String(parm.get("dept_name")));
			up.addWhere("t_user_id", user_id);
			int i = commonMapper.update(up);
			if(i==0){
				retM.put(ApplicationUtils.STATUS, "0");
				retM.put(ApplicationUtils.MSG, "保存失败");
			}
		}else{//新增
			type="新增";
			setLogcategory=1;
			int i = teacherMapper.saveTeacherInfo(parm);
			if(i==0){
				retM.put(ApplicationUtils.STATUS, "0");
				retM.put(ApplicationUtils.MSG, "保存失败");
			}
		}
		TracelogboWithBLOBs traclelogba = MyTools.getOaLogByRequest(request);
		traclelogba.setEntitytypename("t_teacher");
		traclelogba.setEntityid(user_id);
		traclelogba.setLogcategory(setLogcategory);
		traclelogba.setLogdescription(MyTools.getChangeByMap(parm, type));
		int effect=tracelogboMapper.insert(traclelogba);
		System.out.println(effect);
		return retM;
	}

}
